home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 4 / MacMania 4.toast / / Tools&Utilities / MathPad 2.4 / Documentation / MathPad.doc < prev    next >
Text File  |  1996-04-08  |  19KB  |  274 lines

  1.  
  2. --                         MathPad 2.4
  3.  
  4. -- MathPad is a general purpose graphing scientific calculator. It uses a text window rather than simulating buttons on a hand held calculator. This live scratchpad interface allows you to see and edit your entire calculation.
  5.  
  6. -- This file lists the basic capabilities of MathPad mainly by giving short examples. This entire file can be evaluated by MathPad but it is probably more convenient to open a new window, copy a single example at at time and experiment with it.
  7.  
  8. -- The "Examples" folder included with this distribution contains some longer examples and utility functions. These examples show how to implement such things as equation solving, curve fitting, vector calculations and numerical solution of differential equations.
  9.  
  10. ---------- Using MathPad as a calculator
  11. -- MathPad may be used as a calculator simply by typing in numbers and operators. When the ENTER key is hit, each line is evaluated and the results are inserted into the text.
  12.  
  13.    2*(3+4):14.0
  14.  
  15. -- MathPad inserts the ":" and the result "14.0". A status line at the bottom of the window will show a "√" to indicate that evaluation is complete. If any problems are encountered, they will be reported in the status line and the cursor will be moved to the problem in the text. You can edit and reevaluate the text at any time.
  16. -- The RETURN key simply terminates a line. It does not cause any evaluation. The ENTER key is on the numeric keypad (or next to the space bar on powerbook keyboards).
  17. -- An expression will be continued if the line ends with an operator
  18.  
  19.    2 *
  20.    (3+4):14.0
  21.  
  22. -- More than one expression can be placed on a line if they are separated by semicolons.
  23.  
  24.    2*3+4:10.0;  2.1*(.3+4.2):9.4
  25.  
  26. -- The character # may be used to reference the last result
  27.  
  28.    10*234:2340.0
  29.     #+5:2345.0
  30.  
  31. -- Numbers may be entered in "e" format as in "12.3e-4".  The following letter suffixes may also be used: G for e9, M for e6, k for e3, m for e-3, u or µ for e-6, n for e-9, p for e-12.
  32. -- Hex numbers must be preceded by 0x as in 0xAF23. Hex input is treated as a bit pattern for a 32-bit signed integer.
  33. -- The constant π can be entered via the menu or by typing option-p or "pi".
  34. -- Numeric output format can be controlled via the menu.
  35.  
  36. ------------ Comments
  37. -- MathPad ignores text between "--" and the next RETURN. This file is made up mostly of comments.
  38. -- All text between ~ characters is ignored and can span multiple lines.
  39. -- A line beginning with --¶ will cause a page break on printout (¶ is option 7).
  40.  
  41. ------------ Equations
  42. -- MathPad allows the user to type in equations using variables that will be given numeric values elsewhere in the text.
  43.  
  44.    force=mass*accel
  45.    force:160.0
  46.    mass=5
  47.    accel=32
  48.  
  49. -- MathPad evaluates the entire text so numeric values can be given before or after they are used.
  50. -- When MathPad cannot calculate a numeric value from the information given, it will print '?' and the name of the variable whose value is unknown.
  51.  
  52.    a=b+c
  53.    d=a*4
  54. -- d:? b?
  55.  
  56. -- MathPad does not perform algebra. The equation x=y+z (or y+z=x) tells how to compute x given y and z but MathPad does not derive any information about how to compute values for y or z from x.
  57.  
  58. ---------- Functions
  59. -- MathPad has most of the usual built-in functions: abs() acos() asin() atan() atan2() cos() exp() ln() log() sin() sqrt() tan(). Built-in functions may either be typed in or selected from the menu.
  60. -- The argument units for trig functions can be changed to either degrees or radians with the MathPad menu.
  61. -- The value "Radians" can be used for trig calculations that are independent of the Degrees/Radians option choice. The value of Radians is 1 when radians are selected and 57.3 when degrees are selected.
  62.  
  63.    cos(pi*Radians):-1.0; -- -1 for either Degrees/Radians setting
  64.  
  65. -- The built-in function trunc(n) returns the integer part of n.
  66. -- The built-in function sum(expr,var,lower,upper) performs a summation of "expr" while incrementing "var" between "lower" and "upper". The expression given as the first parameter is reevaluated for each step. Expressions for "lower" and "upper" are evaluated only once.
  67. -- The built-in function rand(n) returns a random number in the range 0 to n. More than 10^9 different values are possible. A rand(0) call restarts the sequence.
  68. -- The functions count(),det(),read() and write() operate on arrays and are described in a later section.
  69.  
  70. -- Users can also define their own functions. Functions differ from equations in that their parameter names refer to temporary variables that can take on different values each time the function is called.
  71.  
  72.    Force(mass,accel)=mass*accel
  73.    Force(10,12):120.0
  74.    Force(6,8):48.0
  75.    mass*accel:160.0;    -- uses global values from previous example
  76.  
  77. -- In this example the parameters "accel" and "mass" are separate from the global variables "accel" and "mass".  Any variables used in the definition that do not appear in the parameter list will refer to global variables.
  78. -- Functions can be referenced before they are defined.
  79. -- All references to a function must have the same number of parameters.
  80. -- Multiple definitions of function names are not allowed.
  81.  
  82. -- MathPad's built-in function menu can be extended by placing "XFun" files in it's folder. See the "XFuns" folder for more details.
  83.  
  84. ----------- Conditionals
  85. -- The value of an expression can be made dependent on a condition.
  86.  
  87.    p = -a when a < 0,
  88.         1 when a = 0,
  89.         a^2 otherwise
  90.  
  91. -- The result will be the value corresponding to first condition in the list that evaluates true. Commas separate expressions in the list.
  92. -- The keyword "otherwise" is optional and is used only to help readability.
  93. -- The conditional follows the keyword "when" and may use the following operators: =  !=   >  <  >=  <=  "and"  "or"  "not"  "known"  "unknown". The option key versions of comparison operators are also accepted ≠, ≥, ≤. Note that in this context "=" is a comparison operator. The C style operator "==" is also accepted.
  94. -- The prefix operators "known" and "unknown" can be used to test if a numeric value can be calculated for the given expression. Note that a expression can be 'known' even if its value is NAN04 (not a number). NAN04 results from calculations such as 0/0.
  95. -- Comma separated expressions can also be used without conditionals. The result will be the first item in the list that evaluates to a numeric value.
  96. -- An arithmetic expression is considered true if it is non-zero.
  97. -- The arithmetic result of a comparison will be 0 for false and 1 for true.
  98.  
  99.  
  100. -------- Recursion
  101. -- MathPad allows recursive function definitions. Depth of recursion is limited by available memory space. If memory runs low during evaluation, recursion is stopped.
  102. -- Evaluation can also be stopped by typing Command-period.
  103.  
  104.    fact(n) = 1 when n < 2, fact(n-1)*n when n > 0
  105.    fact(50):3.0e+64
  106.  
  107. --------- Plots
  108. -- MathPad can produce simple plots by using the  "plot" command.
  109.  
  110.     Xmin=0;  Xmax=20
  111.     Ymin=0;  Ymax=Xmax^2
  112.     plot X^2
  113.  
  114. -- Plotting is controlled by special variables. The independent variable "X" is stepped between "Xmin" and "Xmax"  with a resolution of "Xsteps" (default Xsteps=100). Other variables such as Xdiv, Xlabel, Ymin, Ymax, Ydiv, Ylabel, Title and Ystrips can be used to control the plot parameters.
  115. -- The plot command can also be used to plot data points, parametric equations etc.  See the file "Plotting" for more details.
  116. -- Evaluation can be stopped by typing Command-period.
  117.  
  118. ---------- Tables
  119. -- MathPad can produce tables of numeric results using the "table" command.
  120. -- Multiple column tables can be produced by giving a list of expressions separated by commas.
  121.  
  122.  Nmin=1;  Nmax=5;     Nsteps=5
  123.  table    N,       N^2,       N^3
  124.         1.0        1.0        1.0
  125.         2.0        4.0        8.0
  126.         3.0        9.0       27.0
  127.         4.0       16.0       64.0
  128.         5.0       25.0      125.0
  129.  
  130. -- Tables are handled in the same way as plots except that the results are inserted as columns in the text window. For tables the independent variable is "N" and the control variables are "Nmin",  "Nmax"  and "Nsteps".
  131. -- Nsteps is limited to 1000 to prevent accidents from inserting huge amounts of text. Use the write() function for large tables.
  132. -- Do not add any text to the output columns because the table command will not be able to properly delete the table before reevaluating.
  133. -- If table is used with a single expression that is a 1D or 2D array, the array elements will be printed out. The values of Nmin,Nmax and Nsteps are ignored.
  134.  
  135. ------------------- Arrays -------------
  136.  A={10,20,30}      -- Defines a 3 element array.
  137.  A[2]:20.0;     -- Accesses the 2nd element. Index values start at 1.
  138.  B={A,{40,50,60},A+1}   -- Multidimensional array.
  139.  B:{{10.0,20.0,30.0},{40.0,50.0,60.0},{11.0,21.0,31.0}}
  140.  B[1]:{10.0,20.0,30.0}
  141.  B[1][2]:20.0
  142.  B[1,2]:20.0;        -- Both forms of indexing are allowed
  143.  
  144.  A[2:3]:{20.0,30.0}; -- [lo:hi] selects a sub array.
  145.  A[2:3][2:2]:{30.0}; -- consecutive selections operate on the same dimension
  146.  A[:2]:{10.0,20.0};  -- lo and/or hi may be omitted.
  147.  B[2:]:{{40.0,50.0,60.0},{11.0,21.0,31.0}}
  148.  
  149.  Q[i]=i*11  --Arrays can be defined in terms of their index values.
  150.  Q[2]:22.0
  151.  
  152. --dim[] is used to set the number of elements for an array definition. If the expression for number of elements is unknown or < 0, that dimension becomes infinite. dim[0] is equivalent to dim[1].
  153.  I[i,j] = 1 when i=j,0 otherwise   dim[3,3]
  154.  I:{{1.0,0.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0}}
  155.  
  156. --Arrays may be used freely in expressions. Operations are performed on each element. Scalars are extended to contain as many elements as needed.
  157.  A+Q:{21.0,42.0,63.0}
  158.  2+A:{12.0,22.0,32.0}
  159.  log(A):{1.0,1.3,1.5}
  160.  A*Q:{110.0,440.0,990.0}; --Note: this is not matrix multiply
  161. -- Q[A]:? A[]?;           --Arrays may not be used as index values.
  162.  
  163. -- For large arrays only the first few elements in each dimension are printed followed by ... to indicate that there are more elements.
  164.  Q:{11.0,22.0,33.0,...}
  165. --Use table to output medium sized 1D or 2D arrays.
  166.  table B
  167.        10.0       20.0       30.0
  168.        40.0       50.0       60.0
  169.        11.0       21.0       31.0
  170.  
  171. --Functions can use index values
  172.  skip(zz,by)[i] = zz[i*by]
  173.  skip(Q,2):{22.0,44.0,66.0,...}
  174.  multiply(A,B)[i,j] = sum(A[i,k]*B[k,j],k,1,count(B)) dim[count(A),count(B[1])]
  175.  multiply({A},I):{{10.0,20.0,30.0}}
  176.  
  177. --------- Built-in functions for arrays
  178.  count(A):3.0;  -- count() returns the number of array elements
  179.   count(B):3.0;     -- elements in 1st dimension
  180.   count(42):0.0;    -- scalar
  181.   count(Q):?;       -- infinite array
  182.  
  183.  det(B):-0.0;  -- calculates the determinant of a square matrix
  184.  
  185. --------- Data Files
  186. -- read("filename") returns an array of values read from the named data file. Double quotes are needed if the filename contains blanks or punctuation. The data file is assumed to be in the same folder as the source document. Mac style path names such as "Hard Disk:DataFolder:file47" are allowed.
  187. -- write("filename",array)  writes the elements of the 1D or 2D array to the named file.
  188. -- See "Datafiles" for more information.
  189.  
  190.  
  191. --------- Image display
  192. -- The "image" command can be used to display a 2D array. The entire array is displayed and fills the plot area regardless of the values of Xmin, Xmax, Ymin, or Ymax. Scale is controlled by special variables "Zmin" and "Zmax". If Zmin and/or Zmax are not specified MathPad will use Zlo and/or Zhi from the previous evaluation. Display type is controlled by the Image... menu item. For more information see the file "Images"
  193.  
  194.   arr[i,j] = i+j  dim[10,10]
  195.   Zmin=2; Zmax=20
  196.   image arr
  197.  
  198. --------- Assignments
  199. -- The assignment operator := evaluates the right hand side expression and replaces any previous definition of the left hand variable with the result. Accessing an assigned variable gives its current value and does not reevaluate the expression. Assigned variables do not necessarily have the same value everywhere in the text. This differs from = which gives a single definition of how to calculate the value of a variable. 
  200.  
  201.   aa:=3:
  202.   aa:3.0
  203.   aa:=aa+2:
  204.   aa:5.0
  205.   aa:={1,2,aa}:
  206.   aa:{1.0,2.0,5.0}
  207.   aa[2]:=7.6:
  208.   aa:{1.0,7.6,5.0}
  209.  
  210. -- Assignments can only be done to simple global variables.
  211. -- Entire arrays may be assigned (if they are finite).
  212. -- Assignments to array elements are permitted in restricted cases. A single value can be assigned to an element of an array that has previously been initialized by assignment.
  213. -- The result of the := operation itself is always unknown. If you wish to access the value you must use the variable name. This is done so that multiple assignments can be performed using comma separated lists.
  214. -- In cases involving assignments it is sometimes desirable to have evaluation of a sequence continue even if a true "when" clause is encountered. In these cases a double comma can be used to specify that evaluation should continue regardless of the previous clause.
  215.  
  216. aa when 1=1,42:{1.0,7.6,5.0}
  217. zz when 1=1,,42:42.0
  218.  
  219.  
  220. ---------- Iteration
  221. -- The 'while' operator can be used to evaluate an expression repeatedly. The condition is checked before each evaluation. Note: i and k are global.
  222.  
  223. factorial(n) = i:=1,m:=1,(i:=i+1,m:=m*i) while i<n, m
  224. factorial(100):9.3e+157
  225.  
  226. -- See the file "Programming" for more information on assignments and iteration.
  227.  
  228. ---------- Include files
  229. --Definitions in one file can be shared by other files. A document containing the command  include "pathname"  will open and read definitions from the specified file. The folder of the source document will be searched first and then the folder containing the MathPad application. Full or relative path names are allowed. You may want to make a folder containing your favorite include files and put it in MathPad's folder. Then any document can access them with a short relative path name.
  230.  
  231. --A window is opened for each included file. This file can be viewed and edited like any other MathPad document. Any changes made will be seen by the parent document when the parent is reevaluated. Include files should contain only definitions. They can contain other include commands.
  232.  
  233. ---------- Externally compiled functions
  234. -- MathPad XFun files placed in the same folder as the application will be added to the function menu. This allows programmers to add functions written in C and compiled as code resources. Items can also be added to MathPad's Help menu. See the folder "XFuns" for more information.
  235.  
  236. ---------- Technical Details
  237. -- File format
  238. -- MathPad documents are generic TEXT files. Option and plot label information is stored in the resource fork. The resource fork is not modified until the file is saved. TEXT files created by other applications may be opened by MathPad and vice versa. MathPad automatically converts line terminators from UNIX or PC files to Mac format when it reads in a document.
  239. -- Documents are limited to a maximum size of 30K bytes. Data files can be larger than 30K. See "Datafiles".
  240.  
  241. -- Memory usage
  242. -- MathPad's memory requirements can vary widely depending on usage. Simple calculations with small documents use very little memory. Adding XFuns, assigning large arrays, opening several large documents, copying or pasting complex graphics etc. can increase memory requirements considerably. MathPad attempts to warn you of low memory conditions but if you find that you are running near the limit, it is best to use the Finder to increase the size.
  243. -- You can check memory usage with the About MathPad... box. If the unused space drops below 10K, you should increase MathPad's size allocation.  
  244. -- Data files are read into temporary memory. Large data files can be read if there is enough system memory available. Increasing MathPad's size will not allow larger data files but quitting other applications will.
  245. -- Recursive functions can require large amounts of memory but increasing MathPad's size past a certain point will not allow deeper recursion. This is because evaluation of recursive functions also uses up stack space which has a fixed allocation size.
  246.  
  247. -- If you want MathPad to act like a desk accessory, turn on the "Save and Quit" option via the Options... menu and save a document in the Apple Menu Items folder. For documents saved with this option on, clicking the window's close box will save the current text and quit the application. This is similar to the way desk accessory calculators operate. The option affects only the action of the close box. If you choose Close from the File menu, the document will be closed but the application will stay active.
  248. -- If you want MathPad's close box to act like most applications, leave this option off.
  249.  
  250. -- MathPad checks in its application folder for a file named "defaults". If one is found it is used as stationery for all new documents. This allows setting radians, numeric format, fonts window sizes etc. Create a MathPad document with whatever you want for defaults. (You will have to evaluate a plot to set plot window options). Normally you would delete all the text but default text is allowed. Save the file as "defaults" in MathPad's folder. If you prefer, you can use system 7 Finder to set the file's stationery flag. MathPad doesn't care but it might remind you of its special status.
  251.  
  252. -- Background computation
  253. -- Clicking in another application's window or the desktop will switch MathPad to the background even when it is busy with a computation. MathPad will continue computation in the background.
  254.  
  255. -- Incremental evaluation
  256. --MathPad normally evaluates the entire document when the enter key is pressed. This allows you to edit anything anywhere in the text and have everything updated properly. It tries to take advantage of the fact that usually when text is added at the very end of the document no changes are needed in the part of the document that has already been evaluated.
  257. --MathPad will hold off on reevaluating text that has been successfully evaluated. Any edit other than at the very end of the document will cause the entire document to be reevaluated on the next enter.
  258. --If you wish to force reevaluation you can use command R instead of enter. One reason for this might be if you plot something using auto ranging and then later add specifications for Xmin etc. Also, if you hit enter without changing anything in the file it will reevaluate.
  259.  
  260. --  MathPad supports minimal AppleEvents so it can be controlled by AppleScript etc. To provide a quick and dirty means of scripting, the PrintDocuments event will open the document, evaluate it and then print the plot.
  261.  
  262. --------- Distribution
  263. -- MathPad is copyright 1993-1996 by Mark Widholm. It is free for non-commercial distribution. Selling it for profit without my permission is forbidden.
  264.  
  265. -- If you use MathPad, send me some e-mail. I can notify you of any known bugs, updates etc.
  266.  
  267. -- Send comments, questions, suggestions and bug reports to:
  268.  
  269. --       Mark.Widholm@UNH.edu
  270.  
  271. -- Let me know if you are interested in seeing more examples or if you have a MathPad example that others might be interested in seeing. Check out the MathPad web page <http://pubpages.unh.edu/~whd/MathPad/> for more examples and XFuns.
  272.  
  273.  
  274.